package constant.milk.cashbook.asset;

import java.util.ArrayList;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.util.MilkTextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class LinkIdBankActivity extends SuperActivity
{
	public static final String LINK_ASSET = "LINK_ASSET";
	public static final String LINK_DATA = "LINK_DATA";

	private ListView listView;
	private Button checkButton;
	
	private LinkIdListAdapter realListAdapter;
	
	private String items;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.radio_activity);
		
		setGoneTipButton();
		
		setTitle("연결통장");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(LINK_DATA, items);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		Intent intent = getIntent();
		items = intent.getStringExtra(LINK_DATA);
		String linkAsset = intent.getStringExtra(LINK_ASSET);
		
		DBSelectQuery db = new DBSelectQuery(superContext, superDb);
		
		ArrayList<String> tempList = new ArrayList<String>();
		
		if (linkAsset.equals(getString(R.string.bankAsset)))
		{
			ArrayList<BankData> list = db.getBankList();
			
			for (int i = 0; i < list.size(); i++)
			{
				tempList.add(list.get(i).getName());
			}
		}
		else
		{
			ArrayList<CreditData> list = db.getCreditList();
			
			for (int i = 0; i < list.size(); i++)
			{
				tempList.add(list.get(i).getName());
			}
		}
		
		listView.setOnItemClickListener(onItemClickListener);
		
		realListAdapter = new LinkIdListAdapter(
				superContext, R.layout.radio_list_item, tempList);
		listView.setAdapter(realListAdapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			items = (String) arg0.getItemAtPosition(position);
			
			realListAdapter.notifyDataSetChanged();
		}
	};
	
	/**
	 * 리스트 아답터
	 */
	private class LinkIdListAdapter extends ArrayAdapter<String> 
	{
		public LinkIdListAdapter(Context context, int textViewResourceId, ArrayList<String> list) 
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.radio_list_item, null);
			}
			
			String str = getItem(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.radioItemNameTextView);
			RadioButton radioButton = (RadioButton) v.findViewById(R.id.radioItemRadio);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(str);
			
			if (items.equals(str))
			{
				radioButton.setChecked(true);
			}
			else
			{
				radioButton.setChecked(false);
			}
			
			return v;
		}
	}

	@Override
	protected void tipButton()
	{
	}

}
